using System;
using EasyProjects.ClientModel.Entities;

namespace EasyProjects.TimeLogger
{
    public class TaskTimer
    {
        private TimerStatus status = TimerStatus.Stopped;

        public TimerStatus Status
        {
            get { return status; }
        }
        private long ticks = 0;

        public long Ticks
        {
            get { return ticks; }
            set { ticks = value; }
        }

        private Task task;
        private Project project;

        public Task Task
        {
            get { return task; }
        }

        public Project Project
        {
            get { return project; }
        }

        /// <summary>
        /// Returns well formated time string.
        /// </summary>
        public string DisplayTime
        {
            get
            {
                long seconds = ticks % 60;
                long minutes = (ticks / 60) % 60;
                long hours = (ticks / 3600);

                return String.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
            }
        }

        /// <summary>
        /// Start timer
        /// </summary>
        /// <param name="project">Selected project</param>
        /// <param name="task">Selected task</param>
        public void Start(Project project, Task task)
        {
            if (status != TimerStatus.Stopped)
                return;
            this.task = task;
            this.project = project;
            ticks = 0;
            status = TimerStatus.Started;
        }

        /// <summary>
        /// Stop timer
        /// </summary>
        public void Stop()
        {
            status = TimerStatus.Stopped;
            task = null;
            project = null;

        }

        /// <summary>
        /// Pause timer
        /// </summary>
        public void Pause()
        {
            switch (status)
            {
                case TimerStatus.Started:
                    status = TimerStatus.Paused;
                    break;
                case TimerStatus.Paused:
                    status = TimerStatus.Started;
                    break;

            }
        }

        /// <summary>
        /// Increment time if need.
        /// </summary>
        public void Tick()
        {
            if (status == TimerStatus.Started)
                ticks++;
        }

        public override string ToString()
        {
            if ((task != null) && (project != null))
                return String.Format("{0}/{1}", project.Name, task.Name);
            else
                return String.Empty;
        }
    }
}
